package com.michael.demos.reactor;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 类功能描述:
 * <pre>
 *   xxxx
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/8/31 11:34
 */
public class Test {

    private static List<String> words = Arrays.asList(
            "the",
            "quick",
            "brown",
            "fox",
            "jumped",
            "over",
            "the",
            "lazy",
            "dog"
    );

    public static void shortCircuit() {
        Flux<String> helloPauseWorld =
                Mono.just("Hello")
                    .concatWith(Mono.just("world").delaySubscription(Duration.ofMillis(500)));

        helloPauseWorld.subscribe(System.out::println);
    }

    public static void finding() {
        Flux<String> manyLetters = Flux
                .fromIterable(words)
                .flatMap(word -> Flux.fromArray(word.split("")))
                .concatWith(Mono.just("s"))
                .distinct()
                .sort()
                .zipWith(
                        Flux.range(1, Integer.MAX_VALUE),
                        (string, count) -> String.format("%2d. %s", count, string)
                );

        manyLetters.subscribe(System.out::println);
    }

    public static void simple() {
        Flux<String> fewWords = Flux.just("Hello", "World");
        Flux<String> manyWords = Flux.fromIterable(words);

        fewWords.subscribe(System.out::println);
        System.out.println();
        manyWords.sort(Comparator.comparingInt(String::hashCode))
                 .subscribe(System.out::println);
    }

    public static void main(String[] args) {
        Mono<String> hello = Mono.just("hello");

        System.out.println(hello);

        //simple();
        shortCircuit();
        //finding();
    }
}
